<template>
<nav class="site-navbar disable-select" :class="'site-navbar--' + navbarLayoutType">
  <div class="site-navbar__header">
    <h1 class="site-navbar__brand" @click="$router.push({ name: 'home' })">
      <a class="site-navbar__brand-lg" href="javascript:;">谷粒-admin</a>
      <a class="site-navbar__brand-mini" href="javascript:;">谷粒</a>
    </h1>
  </div>
  <div class="site-navbar__body clearfix">
    <el-menu class="site-navbar__menu" mode="horizontal">
      <el-menu-item class="site-navbar__switch" index="0" @click="sidebarFold = !sidebarFold">
        <icon-svg name="zhedie"></icon-svg>
      </el-menu-item>
    </el-menu>
    <el-menu class="site-navbar__menu site-navbar__menu--right" mode="horizontal">
      <el-menu-item index="1" @click="$router.push({ name: 'theme' })">
        <template slot="title">
          <el-badge>
            <!-- value="new" -->
            <span>主题设置
              <icon-svg name="shezhi" class="el-icon-setting"></icon-svg>
            </span>
          </el-badge>
        </template>
      </el-menu-item>
      <el-menu-item index="2">
        <el-badge>
          <!-- value="hot" -->
          <a href="http://127.0.0.1:8848/nacos" target="_blank">Nacos注册中心</a>
        </el-badge>
      </el-menu-item>
      <el-submenu index="3">
        <template slot="title">文档资源</template>
        <el-menu-item index="3-1"><a href="https://gitee.com/aqiang9/gulimall" target="_blank">git源码</a></el-menu-item>
        <el-menu-item index="3-2"><a href="https://easydoc.xyz/s/78237135/ZUqEdvA4/4XuREIJe" target="_blank">接口文档</a></el-menu-item>
        <el-menu-item index="3-3"><a href="http://www.dmanywhere.cn/" target="_blank">数据库表</a></el-menu-item>
        <el-menu-item index="3-4"><a href="https://element.eleme.cn/#/zh-CN/component/tag" target="_blank">ElementUi</a></el-menu-item>
        <el-menu-item index="3-5"><a href="https://doc.xiaominfo.com/" target="_blank">knife4j手册</a></el-menu-item>
        <el-menu-item index="3-6"><a href="https://mp.baomidou.com/guide/#%E7%89%B9%E6%80%A7" target="_blank">mybatis-plus文档</a></el-menu-item>
      </el-submenu>
      <el-submenu index="4">
        <template slot="title">工具资源</template>
        <el-menu-item index="4-1"><a href="http://json.cn/" target="_blank">JSON工具</a></el-menu-item>
        <el-menu-item index="4-2"><a href="https://easydoc.xyz/s/78237135/ZUqEdvA4/4XuREIJe" target="_blank">接口文档</a></el-menu-item>
        <el-menu-item index="4-3"><a href="http://www.dmanywhere.cn/" target="_blank">数据库表</a></el-menu-item>
        <el-menu-item index="4-4"><a href="https://element.eleme.cn/#/zh-CN/component/tag" target="_blank">ElementUi</a></el-menu-item>
      </el-submenu>
      <el-menu-item class="site-navbar__avatar" index="3">
        <el-dropdown :show-timeout="0" placement="bottom">
          <span class="el-dropdown-link">
            <img src="~@/assets/img/avatar.png" :alt="userName">{{ userName }}
          </span>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="updatePasswordHandle()">修改密码</el-dropdown-item>
            <el-dropdown-item @click.native="logoutHandle()">退出</el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </el-menu-item>
    </el-menu>
  </div>
  <!-- 弹窗, 修改密码 -->
  <update-password v-if="updatePassowrdVisible" ref="updatePassowrd"></update-password>
</nav>
</template>

<script>
import UpdatePassword from './main-navbar-update-password'
import { clearLoginInfo } from '@/utils'
export default {
  data() {
    return {
      updatePassowrdVisible: false,
      submenu: [{
        title: "文档资源",
        menu: [{
          url: "https://gitee.com/aqiang9/gulimall",
          msg: "git源码",
          openType: "_blank"
        }, {
          url: "https://easydoc.xyz/s/78237135/ZUqEdvA4/4XuREIJe",
          msg: "接口文档",
          openType: "_blank"
        }]
      }, {
        title: "",
        menu: [{
          url: "https://easydoc.xyz/s/78237135/ZUqEdvA4/4XuREIJe",
          msg: "接口文档",
          openType: "_blank"
        }]
      }]
    }
  },
  components: {
    UpdatePassword
  },
  computed: {
    navbarLayoutType: {
      get() { return this.$store.state.common.navbarLayoutType }
    },
    sidebarFold: {
      get() { return this.$store.state.common.sidebarFold },
      set(val) { this.$store.commit('common/updateSidebarFold', val) }
    },
    mainTabs: {
      get() { return this.$store.state.common.mainTabs },
      set(val) { this.$store.commit('common/updateMainTabs', val) }
    },
    userName: {
      get() { return this.$store.state.user.name }
    }
  },
  methods: {
    // 修改密码
    updatePasswordHandle() {
      this.updatePassowrdVisible = true
      this.$nextTick(() => {
        this.$refs.updatePassowrd.init()
      })
    },
    // 退出
    logoutHandle() {
      this.$confirm(`确定进行[退出]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sys/logout'),
          method: 'post',
          data: this.$http.adornData()
        }).then(({ data }) => {
          if (data && data.code === 0) {
            clearLoginInfo()
            this.$router.push({ name: 'login' })
          }
        })
      }).catch(() => {})
    }
  }
}
</script>
